package cm.kafka.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

/*
 * @author kingboy
 * @Date 2017/7/22 下午2:12
 * @Description LocalDateTimeUtils is used to Java8中的时间类
 */
public class LocalDateTimeUtils {

    public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    //获取当前时间的LocalDateTime对象
    //LocalDateTime.now();

    //根据年月日构建LocalDateTime
    //LocalDateTime.of();

    //比较日期先后
    //LocalDateTime.now().isBefore(),
    //LocalDateTime.now().isAfter(),

    //Long转换为localDateTime
    public static LocalDateTime convertLongToLDT(Long date) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
    }

    //Date转换为LocalDateTime
    public static LocalDateTime convertDateToLDT(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    //LocalDateTime转换为Date
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }


    //获取指定日期的毫秒
    public static Long getMilliByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    //获取指定日期的秒
    public static Long getSecondsByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    //获取指定时间的指定格式
    public static String formatTime(LocalDateTime time, String pattern) {
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    //获取当前时间的指定格式
    public static String formatNow(String pattern) {
        return formatTime(LocalDateTime.now(), pattern);
    }

    //日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
    public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
        return time.plus(number, field);
    }

    //日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
    public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field) {
        return time.minus(number, field);
    }

    /**
     * 获取两个日期的差  field参数为ChronoUnit.*
     *
     * @param startTime
     * @param endTime
     * @param field     单位(年月日时分秒)
     * @return
     */
    public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS) return period.getYears();
        if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
        return field.between(startTime, endTime);
    }

    //获取一天的开始时间，2017,7,22 00:00
    public static LocalDateTime getDayStart(LocalDateTime time) {
        return time.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
    }

    //获取一天的结束时间，2017,7,22 23:59:59.999999999
    public static LocalDateTime getDayEnd(LocalDateTime time) {
        return time.withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);
    }

    public static void main(String[] args) {
        DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalDateTime startTime = LocalDateTime.of(2020,11,1,10,12,12);
        startTime = startTime.with(ChronoField.DAY_OF_WEEK, 1);

        System.out.println(startTime);
    }

    public static String getType(LocalDateTime startTime, LocalDateTime endTime) {
        String type = "D";
        if (startTime.plusDays(7).isAfter(endTime) || startTime.plusDays(7).isEqual(endTime)) {//查询时间范围为7天内
            type = "H";
        }
        if (startTime.plusDays(1).isAfter(endTime) || startTime.plusDays(1).isEqual(endTime)) {//查询时间范围为一天内
            type = "T";
        }
        if (startTime.plusHours(1).isAfter(endTime) || startTime.plusHours(1).isEqual(endTime)) {//查询时间范围为1小时内
            type = "M";
        }
        return type;
    }

    public static Map<String, Long> generateTimePoint(LocalDateTime startTime, LocalDateTime endTime, String type) {
        Map<String, Long> timeMap = new TreeMap<>();

        LocalDateTime left = timestampByFormat(startTime, type);
        LocalDateTime right = timestampByFormat(endTime, type);

        int index = 0;
        LocalDateTime temp;
        boolean result = true;
        do {

            if (type.equals("D")) {
                temp = left.plusDays(index);
                if (temp.isAfter(right)) {
                    result = false;
                } else {
                    timeMap.put(temp.format(formatter), 0L);
                }
            } else if (type.equals("H")) {
                temp = left.plusHours(index);
                if (temp.isAfter(right)) {
                    result = false;
                } else {
                    timeMap.put(temp.format(formatter), 0L);
                }
            } else if (type.equals("T")) {
                temp = left.plusMinutes(index*10);
                if (temp.isAfter(right)) {
                    result = false;
                } else {
                    timeMap.put(temp.format(formatter), 0L);
                }
            } else {
                temp = left.plusMinutes(index);
                if (temp.isAfter(right)) {
                    result = false;
                } else {
                    timeMap.put(temp.format(formatter), 0L);
                }
            }
            index++;
        } while(result);

        return timeMap;
    }

    public static LocalDateTime timestampByFormat(LocalDateTime dateTime, String type) {
        LocalDateTime temp;
        // 将时分秒,毫秒域清零
        if (type.equals("D")) {
            temp = LocalDateTime.of(dateTime.toLocalDate(), LocalTime.of(0,0,0));
        } else if (type.equals("H")) {
            temp = LocalDateTime.of(dateTime.toLocalDate(), LocalTime.of(dateTime.getHour(),0,0));
        } else if (type.equals("T")) {
            int minute = dateTime.getMinute();
            if (minute <= 10) {
                temp = LocalDateTime.of(dateTime.toLocalDate(), LocalTime.of(dateTime.getHour(),10,0));
            } else if (minute > 10 && minute <= 20) {
                temp = LocalDateTime.of(dateTime.toLocalDate(), LocalTime.of(dateTime.getHour(),20,0));
            } else if (minute > 20 && minute <= 30) {
                temp = LocalDateTime.of(dateTime.toLocalDate(), LocalTime.of(dateTime.getHour(),30,0));
            } else if (minute > 30 && minute <= 40) {
                temp = LocalDateTime.of(dateTime.toLocalDate(), LocalTime.of(dateTime.getHour(),40,0));
            } else if (minute > 40 && minute <= 50) {
                temp = LocalDateTime.of(dateTime.toLocalDate(), LocalTime.of(dateTime.getHour(),50,0));
            } else {
                temp = LocalDateTime.of(dateTime.toLocalDate(), LocalTime.of(dateTime.getHour(),0,0));
            }
        } else {
            temp = LocalDateTime.of(dateTime.toLocalDate(), LocalTime.of(dateTime.getHour(),dateTime.getMinute(),0));
        }
        return temp;
    }



}